package com.eliaukjq.javase;


import java.sql.*;

/**
 * @Author : 佩萁
 * Description :
 * @Create : 2023/11/16 - 19:27
 */
public class JdbcExample {
    public static void main(String[] args) {
        // JDBC 连接信息
        String jdbcUrl = "jdbc:mysql://localhost:3306/yupi";
        String username = "root";
        String password = "Mysql.123";

        // JDBC 驱动名和数据库 URL
        String jdbcDriver = "com.mysql.cj.jdbc.Driver";

        // 加载 JDBC 驱动
        try {
            Class.forName(jdbcDriver);
        } catch (ClassNotFoundException e) {
            System.err.println("Failed to load JDBC driver.");
            e.printStackTrace();
            return;
        }

        try (
                // 建立连接
                Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
                // 执行 SQL 查询
                PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM user  limit 1 offset 3");
                ResultSet resultSet = preparedStatement.executeQuery()
        ) {
            // 处理查询结果集
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("username");
                System.out.println("ID: " + id + ", Name: " + name);
            }
        } catch (SQLException e) {
            System.err.println("Database error.");
            e.printStackTrace();
        }
    }
}
